package com.demon.activitychange.xposed;

import android.app.Application;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.widget.Toast;

import com.demon.activitychange.util.TraceUtil;

import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

public class HookMain implements IXposedHookLoadPackage {
    // 包名
    public static final String PACKAGE_ID_NORM = "com.ss.android.ugc.aweme"; // 抖音短视频
    public static final String PACKAGE_ID_LITE = "com.ss.android.ugc.aweme.lite"; // 抖音极速版

    // 标识
    public enum DyOperEnum {
        // 基础
        unknown(0, "", ""),
        exit(1, "dy.user.clear.exit.app", "dy.user.clear.exit.app.result"),

        // 极速版
        // 密码登录
        login(2, "dy.passport.login", "dy.passport.login.result"),
        loginVerify(3, "dy.passport.login.verify", "dy.passport.login.verify.result"),
        // 发送验证码
        sendCode(4, "dy.passport.send.code", "dy.passport.send.code.result"),
        // 验证码登录
        smsLogin(5, "dy.passport.sms.login", "dy.passport.sms.login.result"),
        // 验证码登录+修改密码
        smsReg(6, "dy.passport.sms.reg", "dy.passport.sms.reg.result"),
        // 修改密码验证码确认
        smsUpdatePassword(7, "dy.passport.sms.update.password", "dy.passport.sms.update.password.result"),

        // 正常版
        // 私信
        message(8, "dy.im.send.msg", "dy.im.send.msg.result");

        private int value = 0;
        private String dyBroadcast = "";
        private String dyResult = "";

        /**
         * @param value       操作标识
         * @param dyBroadcast dy收到的广播
         * @param dyResult    dy发出的广播
         */
        DyOperEnum(int value, String dyBroadcast, String dyResult) {
            this.value = value;
            this.dyBroadcast = dyBroadcast;
            this.dyResult = dyResult;
        }

        public int getValue() {
            return value;
        }

        public String getBroadcast() {
            return dyBroadcast;
        }

        public String getResult() {
            return dyResult;
        }
    }

    //是否已经HOOK
    private boolean isHook = false;

    public void handleLoadPackage(XC_LoadPackage.LoadPackageParam lpparam) throws Throwable {
        if (lpparam.appInfo == null || (lpparam.appInfo.flags & (ApplicationInfo.FLAG_SYSTEM | ApplicationInfo.FLAG_UPDATED_SYSTEM_APP)) != 0) {
            return;
        }
        final String packageName = lpparam.packageName;
        final String processName = lpparam.processName;
        if (PACKAGE_ID_LITE.equals(packageName)) {
            try {
                XposedHelpers.findAndHookMethod(Application.class, "attach", Context.class, new XC_MethodHook() {
                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                        super.afterHookedMethod(param);
                        final Context context = (Context) param.args[0];
                        ClassLoader appClassLoader = context.getClassLoader();
                        if (PACKAGE_ID_LITE.equals(processName) && !isHook) {
                            isHook = true;
                            TraceUtil.xe("助手初始化成功");
                            Toast.makeText(context, "助手初始化成功", Toast.LENGTH_LONG).show();
                            new HookLiteHandler().hook(appClassLoader, context);
                        }
                    }
                });
            } catch (Throwable e) {
                TraceUtil.xe(e.getMessage());
            }
        } else if (PACKAGE_ID_NORM.equals(packageName)) {
            try {
                XposedHelpers.findAndHookMethod(Application.class, "attach", Context.class, new XC_MethodHook() {
                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                        super.afterHookedMethod(param);
                        final Context context = (Context) param.args[0];
                        ClassLoader appClassLoader = context.getClassLoader();
                        if (PACKAGE_ID_NORM.equals(processName) && !isHook) {
                            isHook = true;
                            TraceUtil.xe("助手初始化成功");
                            Toast.makeText(context, "助手初始化成功", Toast.LENGTH_LONG).show();
                            new HookNormHandler().hook(appClassLoader, context);
                        }
                    }
                });
            } catch (Throwable e) {
                TraceUtil.xe(e.getMessage());
            }
        }
    }
}
